package com.nowcoder.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nowcoder.entity.Message;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface MessageMapper extends BaseMapper<Message> {

    @Select("select * from `message` where id in " +
            "( select max(id) from `message` where status != 2 and from_uid != \"1\" and (from_uid = #{uid} or to_uid = #{uid}) group by conversation_id ) " +
            "order by id desc limit #{offset}, #{limit}")
    public List<Message> selectConversations(String uid, int offset, int limit);

    @Select("select count(m.maxid) from " +
            "( select max(id) as maxid from `message` where status != 2 and from_uid != \"1\" and (from_uid = #{uid} or to_uid = #{uid}) group by conversation_id ) " +
            " as m")
    public int selectConversationCount(String uid);

    @Select("select * from `message` where status != 2 and from_uid != \"1\" and conversation_id != #{conversationId} order by id desc limit #{offset}, #{limit}")
    public List<Message> selectLetters(String conversationId, int offset, int limit);

    @Select("select count(id) from `message` where status != 2 and from_uid != \"1\" and conversation_id != #{conversationId}")
    public int selectLetterCount(String conversationId);

    @Select("select count(id) from `message` where status = 0 and from_uid != \"1\" and to_uid = #{uid} and conversation_id = #{conversationId}")
    public int selectLetterUnreadCount(String uid, String conversationId);
}
